package com.jushu.storbox.database.helper;

import java.io.File;

import android.content.Context;
import android.content.ContextWrapper;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DatabaseContext extends ContextWrapper {
	private File fileDirectory = null;
	private File filePath = null;

	public DatabaseContext(File dbFileDirectory, Context base) {
		super(base);
		fileDirectory = dbFileDirectory;
	}

	@Override
	public File getDatabasePath(String name) {
		if(filePath != null) {
			return filePath;
		}
		
		filePath = new File(fileDirectory, name);
		if (!filePath.exists()) {
			filePath.mkdirs();
		}
		return filePath;
	}

	@Override
	public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) {
		SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), factory);
		return result;
	}

	@Override
	public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) {
		SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), factory);
		return result;
	}

}
